/**
 * 功能描述
 */

define(function (require, exports, module) {
	var Grid = require("grid");
	var aw = require("ajaxwrapper");
	var Dialog = require("dialog");
	var store =require("store");
	var activeUser = store.get("user");
	var Form = require("form");
    var instance = {
        initComponent: function (params,widget) {
        	var grid = new Grid({
        		parentNode: ".J-grid",
        		model:{
        			url:"api/costprice/query",
        			params:function(){
        				var subnav = widget.get("subnav");
        				return{
        					"flatCostStatus":subnav.getValue("status"),
        					"orderString" : "operateDate:ASC",
        					fetchProperties:"*,travelContract.memberCertificates,travelContract.productType.name,travelContract.productRule.product.name,travelContract.member.personalInfo.pkPersonalInfo,travelContract.member.personalInfo.name," +
        							"travelContract.member.personalInfo.idNumber"
        				}
        			},
        			columns:[{
        				name:"travelContract.memberCertificates",
        				label:"权证号",
        				format : function(value, row) {
        					var travelContract = row.travelContract;
        					if(travelContract){
        						return travelContract.productRule.product.name+"-"+travelContract.productType.name+"-"+travelContract.memberCertificates
        					}
        				}
        			},{
        				name:"travelContract.member.personalInfo.name",
        				label:"姓名"
        			},{
        				name:"travelContract.member.personalInfo.idNumber",
        				label:"身份证号"
        			},{
        				name:"operateDate",
        				format:"date",
        				label:"办理日期"
        			},{
        				name:"flatCost",
        				label:"工本费",
        				format:function(val){
							return Number(val).toFixed(2);
						}
        			},{
        				name:"cause",
        				label:"原因"
        			},{
        				name:"flatCostStatus",
        				label:"状态",
        				format:function(flatCostStatus){
        					if(flatCostStatus.key=="Done"){
        						return "已收费";
        					}else{
        						return "待收费";
        					}
        				}
        			},{
        				name:"operate",
        				label:"操作",
        				format:"button",
        				formatparams:[{
        					id:"open",
        					text:"收费",
        					show:function(index,data,rowEle){
        						var flag = true;
        						
        						if(flag){
        							return true;
        						}
							},
							handler:function(index,data,rowEle){
								
								if(data.flatCostStatus.key=="Done"){
									Dialog.alert({
										title : "提示",
										content : "已经收费完成，无法继续收费！"
									});
									return false
									
								}
								
								var remarkForm = new Form({
        							parentNode:".J-dialog-conponent",
        							autoRender:false,
        							model:{
        								defaultButton:false,
        								id:"remarkForm",
        								  items: [{
	        							            name: "pkCostPrice",
	        							            type: "hidden",
	        							            defaultValue :data.pkCostPrice
        							            
        								  		},{
	        							            name: "version",
	        							            type: "hidden",
        								  		},{
    												name:"price",
    												label:"金额(元)",
    												validate : ["required"],
    								                exValidate: function(value) {
    								                	var reg = /^([1-9][\d]{0,7}|0)(\.[\d]{1,2})?$/;
    						                            if (!reg.test(value)) {
    						                                return "请输入正确金额！";
    						                            } else {
    						                                return true;
    						                            }
    						                        }, 
    											},{
        								  			name:"flatCostDate",
    												label:"收费日期",
    												type:"date",
    												defaultValue :moment(data.checkInDate).format("YYYY-MM-DD"),
        								  		},{
        								  			name:"operator",
        								  			label:"经办人",
        								  			defaultValue:activeUser.name
        								  		}]
        								}	       			            	
        						});
    							widget.set("remarkForm",remarkForm);
    							Dialog.showComponent(remarkForm,{
    								defaultButton:false,
    								title: "工本费",
    								setStyle: function(){
										$(".el-dialog-modal .modal").css({
											"top": "10%",
											"width":"50%",
											"left": "25%",
										});
									},
    								buttons :[{
    									id:"save",
    									text:"确认",
     									className:"btn-primary",
    									handler:function(){
    										if(remarkForm.valid()){
    											var FormData = remarkForm.getData();
    											var saveData = {};
    											
    											saveData.pkCostPrice = FormData.pkCostPrice;
    											saveData.price=FormData.price;
    											saveData.flatCostDate=FormData.flatCostDate;
    											saveData.operator=activeUser.pkUser;
    											saveData.flatCostStatus="Done";
    											saveData.version=data.version;
    											aw.saveOrUpdate("api/costprice/save",aw.customParam(saveData),function(saveData){
    												Dialog.close();
    							          			widget.get("grid").refresh();
    							       			});
    										};
    									}
    								},{
    									id:"close",
    									text:"取消",
    									handler:function(){
    										Dialog.close();
    									}
    								}]
    							});
							}
        				},{
        					id:"show",
        					text:"查看",
        					handler:function(index,data,rowEle){
        						var  form = widget.get("form")
        						widget.get("subnav").show(["return"]).hide(["search","add","status"]);
        						var cparam={
        								memberCertificates:data.travelContract.memberCertificates,
		    							name:data.travelContract.member.personalInfo.name,
		    							idNumber:data.travelContract.member.personalInfo.idNumber,
		    							price:Number(data.price).toFixed(2),
		    							operateDate:moment(data.operateDate).format("YYYY-MM-DD"),
		        						flatCost:Number(data.flatCost).toFixed(2),
		        						cause:data.cause,
		        						flatCostStatus:data.flatCostStatus,
        						};
        						widget.get("form").setData(cparam);
        						form.setDisabled(true);
        						widget.hide([".J-grid"]).show([".J-form"]);
        					}
        				}]
        			},]
        		},
        		
        	});
        	widget.set("grid",grid)
        }
    };
    module.exports = instance;
});
